!26oct21 - error message as output
!26oct21	FUNCTION rtbis(func,x1,x2,xacc,)
	FUNCTION rtbis(func,x1,x2,xacc,errmess)
!!	USE nrtype; USE nrutil, ONLY : nrerror
	IMPLICIT NONE
	REAL(8), INTENT(IN) :: x1,x2,xacc
	REAL(8) :: rtbis
!!	INTERFACE
!!		FUNCTION func(x)
!!		USE nrtype
!!		IMPLICIT NONE
!!		REAL(prec), INTENT(IN) :: x
!!		REAL(prec) :: func
!!		END FUNCTION func
!!	END INTERFACE
	INTEGER, PARAMETER :: MAXIT=40
	INTEGER :: j
	REAL(8) :: dx,f,fmid,xmid
    integer :: errmess !26oct21
    real(8) :: func
    external func

    errmess=0 !26oct21
	
    fmid=func(x2)
	f=func(x1)
	if (f*fmid >= 0.0) then
        write(*,*) 'rtbis: root must be bracketed '
        errmess=1 !26oct21
!        write(*,*) 'jz jb jd ja ', jz, jb, jd, ja
!26oct21        stop
    end if
	if (f < 0.0) then
		rtbis=x1
		dx=x2-x1
	else
		rtbis=x2
		dx=x1-x2
	end if
	do j=1,MAXIT
		dx=dx*0.5_8
		xmid=rtbis+dx
		fmid=func(xmid)
		if (fmid <= 0.0) rtbis=xmid
		if (abs(dx) < xacc .or. fmid == 0.0) RETURN
	end do
        write(*,*) 'rtbis: too many bisections'
        stop
	END FUNCTION rtbis
